#!/usr/bin/env ruby
#encoding: utf-8

class Presentation 
  attr_reader :subject, :presenter, :team, :link, :present_at, :answer_id, :deleted, :deleted_at, :create_at, :update_at

  def initialize data
    @data = data
    #@data = data.slice(0..300)
    #@data = data.slice(0..200)
    begin
      @answer_id, @thread_id, @deleted, @deleted_at, @added_at, @text = @data.split("\t")
    rescue => e
      raise e
    end

    @link = '/question/175/presentation-tracking-for-within-team-knowledge-sharing/?answer={{answer_id}}#post-id-{{answer_id}}'.gsub(/{{answer_id}}/, @answer_id)
    self.parse_text
  end

  def parse_text
    @text = @text.gsub(/<.*?>/, '')
    @text = @text.gsub(/"/, "'")

    @m = /subject:?(?<m>.*?)\\n/i.match(@text)
    @subject = @m['m'].strip unless @m.nil? or @m['m'].nil?

    @m = /presenter:?(?<m>.*?)\\n/i.match(@text)
    @presenter = @m['m'].strip unless @m.nil? or @m['m'].nil?

    @m = /team:?(?<m>.*?)\\n/i.match(@text)
    @team = @m['m'].strip unless @m.nil? or @m['m'].nil?

  end

  def dump
    if @deleted_at == 'NULL'
      @deleted_time = 'NULL'
    else
      @deleted_time = "\"#{@deleted_at}\""
    end
    "(\"#{@subject}\", \"#{@presenter}\", \"#{@team}\", \"#{@link}\", \"#{@answer_id}\", \"#{@deleted}\", #{@deleted_time}, \"#{@added_at}\", \"#{@added_at}\", \"#{@added_at}\"),"
  end

end

@filename = 'mysql_new.dump'
@list = []
File.foreach(@filename).with_index { |line, line_num|
  unless line_num == 0
  #if [3, 4, 6, 11, 12, 13, 24, 25, 32, 36, 37, 48, 49, 76, 77].include? line_num
  #if [4, 11, 12, 32].include? line_num
    begin
      @p = Presentation.new(line)
      @list << @p
    rescue => e
      puts "line #{line_num} failed"
      puts e.message
    end
  end
}
@list.each do |p|
  puts p.dump
end
